package edu.vt.ece_3574.amber_alert.membership;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.MemberMain;
import edu.vt.ece_3574.amber_alert.PrimaryMain;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;

public class AccountSettingsLoginActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		String currentAccountType = AmberAlertApplication.get().getCurrentAccount().getType();
		if (currentAccountType.equals("Member")) {
			setContentView(R.layout.member_primary_login);
		}
		else {
			setContentView(R.layout.primary_settings_login);
		}
		
		Button loginButton = (Button)findViewById(R.id.loginbutton);
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String result = checkCredentials();
				if(result.equals("Primary")) {
					Intent i = new Intent(AccountSettingsLoginActivity.this, PrimaryAccountSettings.class);
					startActivity(i);
					finish();
				}
				else if (result.equals("Member")) {
					Intent i = new Intent(AccountSettingsLoginActivity.this, MemberAccountSettings.class);
					startActivity(i);
					finish();
				}
				else {
					showFailureMessage(result);
				}
			}
		});
		
		Button returnButton = (Button)findViewById(R.id.returnButton);
		returnButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Account currentAccount = AmberAlertApplication.get().getCurrentAccount();
				if(currentAccount.getType().equals("Member")) {
					Intent i = new Intent(AccountSettingsLoginActivity.this, MemberMain.class);
					startActivity(i);
					finish();
				}
				else if(currentAccount.getType().equals("Primary")) {
					Intent i = new Intent(AccountSettingsLoginActivity.this, PrimaryMain.class);
					startActivity(i);
					finish();
				}
			}
		});
	}
	
	/**
	 * Checks whether the password matches the supplied username
	 * @return whether the username and password match
	 */
	private String checkCredentials(){
		EditText userNameField = (EditText)findViewById(R.id.username);
		EditText passwordField = (EditText)findViewById(R.id.password);
		
		String userNameError = "Username field empty!";
		String passwordError = "Password field empty!";
		String errorMessage = "Wrong username or password.";
		String noUserExistsError = "No user by that name exists.";
		
		String userNameString = userNameField.getText().toString();
		String passwordString = passwordField.getText().toString();
		
		Account currentAccount = AmberAlertApplication.get().getCurrentAccount();
		
		//Return an error if username is not specified
		if(userNameField.length() == 0)
			return userNameError;
		else if(passwordField.length() == 0) //Return an error if the password is not specified
			return passwordError;
		//If logged in as a primary, return an error if the specified username does not match the username of the primary that is logged in
		else if ((currentAccount.getType().equals("Primary")) && (!currentAccount.getName().equals(userNameString))) {
				return errorMessage;
		}
		//If logged in as a primary, return an error if the specified username does not match the username of a primary associated with the user that is logged in
		else if ((currentAccount.getType().equals("Member")) && !currentAccount.getAssociatedAccountNames().contains(userNameString)) {
			if (!currentAccount.getAssociatedAccountNames().contains(userNameString)) {
				return errorMessage;
			}
			if (!DatabaseHandler.get().getAccountFromServerByName(userNameString).getType().equals("Primary")) {
				return errorMessage;
			}
		}
		else if (!DatabaseHandler.get().determineIfUserExistsOnServer(userNameString)) {
			return noUserExistsError;
		}
		
		Account loginAccount = DatabaseHandler.get().checkLoginCredentials(userNameString, passwordString);
		if (loginAccount == null) {
			return errorMessage;
		}
		else {
			return currentAccount.getType();
		}
	}
	
	/**
	 * Shows a failure message if the login information was incorrect
	 * @param string of error message to be displayed
	 */
	private void showFailureMessage(String errorMessage) {
		Context context = getApplicationContext();
		CharSequence message = errorMessage;
		int duration = Toast.LENGTH_LONG;

		Toast toast = Toast.makeText(context, message, duration);
		toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
		toast.show();
	}
	
	public void onBackPressed() { 
		Account currentAccount = AmberAlertApplication.get().getCurrentAccount();
		if(currentAccount.getType().equals("Member")) {
			Intent i = new Intent(AccountSettingsLoginActivity.this, MemberMain.class);
			startActivity(i);
			finish();
		}
		else if(currentAccount.getType().equals("Primary")) {
			Intent i = new Intent(AccountSettingsLoginActivity.this, PrimaryMain.class);
			startActivity(i);
			finish();
		}
		return; 
	}
}
